package stream;

import lambda.cart.CartService;
import lambda.cart.Sku;
import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 常见预定义收集器使用
 * @author zhaowenxu
 * @date 2020/4/7 1:18
 */
public class StreamCollector {

    private List<Sku> list = CartService.getCartSkuList();

    @Test
    public void toList() {
        List<Sku> resultList = list.stream()
                .filter(sku -> sku.getTotalPrice() > 100)
                .collect(Collectors.toList());
        System.out.println(resultList);
    }

    /**
     * 分组
     */
    @Test
    public void group() {
        Map<Enum, List<Sku>> collectMap = list.stream()
                .collect(Collectors.groupingBy(Sku::getSkuCategory));
        System.out.println(collectMap);
    }

    /**
     * 分区
     */
    @Test
    public void partition() {
        Map<Boolean, List<Sku>> collectMap = list.stream()
                .collect(Collectors.partitioningBy(sku -> sku.getTotalPrice() > 100));
        System.out.println(collectMap);
    }
}
